const uuidv1 = require('uuid')

let TWEPOCH = 1451577600446;

let RAND_BITS = 10;

let randMask = -1 ^ (-1 << RAND_BITS);

let SEQUENCE_BITS = 12;

let SEQUENCE_MASK = -1 ^ (-1 << SEQUENCE_BITS);

let sequence = 0;

let lastTimestamp = -1;


function isNotNullAndSpace(s) {
    if (s == undefined) {
        return false
    }

    if (s == "") {
        return false
    }
    return true
}

function isNullOrSpace(s) {
    if (s == undefined) {
        return true
    }

    if (s == null) {
        return true
    }

    if (s == "") {
        return true
    }
    return false
}

function getUUID() {

    let tilNextMillis = function (lastTimestamp) {
        let timestamp = new Date().getTime();
        while (timestamp <= lastTimestamp) {
            timestamp = new Date().getTime();
        }
        return timestamp;
    };


    let rand = 0;


    let timestamp = new Date().getTime();

    if (timestamp < lastTimestamp) {
        console.log(`genUniqueKey error ${timestamp} ${lastTimestamp}`)
    }

    if (timestamp == lastTimestamp) {
        // 当前毫秒内，则+1
        sequence = (sequence + 1) & SEQUENCE_MASK;
        if (sequence == 0) {
            // 当前毫秒内计数满了，则等待下一秒
            timestamp = tilNextMillis(lastTimestamp);
        }
    } else {
        sequence = 0;
    }

    lastTimestamp = timestamp;

    rand = Math.floor(Math.random() * (randMask - 1) + 1);

    let bigNUM = ((BigInt(timestamp) - BigInt(TWEPOCH)) << BigInt(22) | BigInt(sequence) << BigInt(10) | BigInt(rand)).toString();

    if (bigNUM.length <= 12 || Number(bigNUM) <= 0) {
        const s = uuidv1.v1();
        return s.replace(/-/g, '')
    } else {
        return bigNUM;
    }
}

function getUUIDV2() {
    const s = uuidv1.v1();
    return s.replace(/-/g, '')
}

function queryRandomNo(x) {
    let num = Math.floor(Math.random() * (10 ** x))
    num = num.toString()
    const d = x - num.length
    num = (d > 0 ? '0'.repeat(d) : '') + num
    return num
}

function intToFen(x) {
    return (x / 100).toFixed(2)
}

function addMysqlPageSize(sql, pageIndex, pageSize) {
    pageIndex = pageIndex || 1
    pageIndex = parseInt(pageIndex) - 1;
    if (pageIndex < 0) {
        pageIndex = 0;
    }
    pageSize = pageSize || 20
    pageSize = parseInt(pageSize)
    if (pageSize > 10000) {
        pageSize = 10000
    }
    sql += ` limit ${pageIndex * pageSize}, ${pageSize}`
    return sql
}

function getDecimalNum(str) {
    try {
        if (str.toString().split(".")[1]) {
            return str.toString().split(".")[1].length;
            ;
        } else {
            return 0;
        }
    } catch (e) {
        console.error('getDecimalNum error str:', str);
        return 0;
    }
}

function format() {

}

function reverse (str) {
    return str.split('').reverse().join('');
}

function splitName(fullname){
    let hyphenated = ['欧阳', '太史', '端木', '上官', '司马', '东方', '独孤', '南宫', '万俟', '闻人',
        '夏侯', '诸葛', '尉迟', '公羊', '赫连', '澹台', '皇甫', '宗政', '濮阳', '公冶',
        '太叔', '申屠', '公孙', '慕容', '仲孙', '钟离', '长孙', '宇文', '城池', '司徒',
        '鲜于', '司空', '汝嫣', '闾丘', '子车', '亓官', '司寇', '巫马', '公西', '颛孙',
        '壤驷', '公良', '漆雕', '乐正', '宰父', '谷梁', '拓跋', '夹谷', '轩辕', '令狐',
        '段干', '百里', '呼延', '东郭', '南门', '羊舌', '微生', '公户', '公玉', '公仪',
        '梁丘', '公仲', '公上', '公门', '公山', '公坚', '左丘', '公伯', '西门', '公祖',
        '第五', '公乘', '贯丘', '公皙', '南荣', '东里', '东宫', '仲长', '子书', '子桑',
        '即墨', '达奚', '褚师'];
    let hyset = new Set(hyphenated);


    let vLength = fullname.length;
    // 前为姓,后为名
    let lastname = '', firstname = '';
    if (vLength > 2){
        var preTwoWords = fullname.substr(0, 2);
        // 取命名的前两个字,看是否在复姓库中
        if (hyset.has(preTwoWords)){
            lastname = preTwoWords;
            firstname = fullname.substr(2);
        }else{
            lastname = fullname.substr(0, 1);
            firstname = fullname.substr(1);
        }
    }else if (vLength === 2){
        // 全名只有两个字时,以前一个为姓,后一下为名
        firstname = fullname.substr(0, 1);
        lastname = fullname.substr(1);
    }else{
        lastname = fullname;
    }
    return [firstname,lastname];
}

function hasCommonValue(arr1, arr2){
    return arr1.some(function (value) {
        return arr2.includes(value);
    });
}


module.exports = {
    isNotNullAndSpace,
    isNullOrSpace,
    getUUID,
    getUUIDV2,
    queryRandomNo,
    intToFen,
    addMysqlPageSize,
    getDecimalNum,
    format,
    reverse,
    splitName,
    hasCommonValue
}